﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace HackersDiet
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class SeriesManipulatorCollection : CollectionBase, ICustomTypeDescriptor
    {
        public void Add(ISeriesManipulator ism)
        {
            this.List.Add(ism);
        }

        public void Remove(ISeriesManipulator ism)
        {
            this.List.Remove(ism);
        }

        public ISeriesManipulator this[int index]
        {
            get
            {
                return (ISeriesManipulator)this.List[index];
            }
        }

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return GetProperties();
        }

        public PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null);

            for (int i = 0; i < this.List.Count; i++)
            {
                SeriesManipulatorPropertyDescriptor pd = new SeriesManipulatorPropertyDescriptor((ISeriesManipulator)this.List[i]);
                pdc.Add(pd);
            }

            return pdc;
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion
    }

    public class SeriesManipulatorPropertyDescriptor : PropertyDescriptor
    {
        ISeriesManipulator ism = null;

        public SeriesManipulatorPropertyDescriptor(ISeriesManipulator sm) : base(sm.Name, null)
        {
            ism = sm;
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get { return typeof(ISeriesManipulator); }
        }

        public override object GetValue(object component)
        {
            return this.ism;
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type PropertyType
        {
            get { return ism.GetType(); }
        }

        public override void ResetValue(object component)
        {
        }

        public override void SetValue(object component, object value)
        {
            //this.collection[index] = value;
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        //public override string DisplayName
        //{
        //    get
        //    {
        //        return this.collection[index].Name;
        //    }
        //}

        //public override string Description
        //{
        //    get
        //    {
        //        return this.collection[index].Name;
        //    }
        //}
    }
}
